home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Do you have ever pass structures?
- Date: 25 Feb 1996 12:58:59 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gqiijINN5ot@keats.ugrad.cs.ubc.ca>
- References: <4ge8mi$qjm@srvr1.engin.umich.edu> <4gnlf2$1fh@news.mistral.co.uk>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4gnlf2$1fh@news.mistral.co.uk>,
- Mike Barnard <mikebarnard@mistral.co.uk> wrote:
- >Hi.
- >
- >hasdi@news-server.engin.umich.edu (HASDI RODZMANN HASHIM) wrote:
- >
- >>MY QUESTION is do you ever pass the whole structures to a function
- >>or you just pass a pointer to a function? For example, for a struct
- >>of the following size...
- >
- >I'm new at this and not very good. But I have been reading on this
- >subject ant it seems that if you pass the whole structure it takes a
- >lot longer at the whole structure is copied onto the stack. If this is
- >in a loop it could take up valuable code time. Also if you are for
- >some reason close to the end of the stack you could overrun it and
- >have a stack overflow error.
-
- This is largely irrelevant. Pushing a small word onto the stack can blow it
- just as well. Your point is valid in that passing big structures will add a lot
- of bloat to the stack depth of a recursive functions, perhaps needlessly so.
- On the other hand if a private copy of the data is really needed, then you are
- really arguing against the design of the algorithm which calls for that
- requirement.
-
- Modern architectures/operating systems have large stacks, by the way, that are
- limited basically by available virtual memory rather than the possibility of a
- stack/heap collision.
-
- >If you pass a pointer only the value of the pointer is put on the
- >stack. Saves much time and stack space.
-
- Buy you may need a local copy of the structure. Suppose I had:
-
- typedef struct complex {
- double real;
- double imaginary;
- } complex_t;
-
- complex_t my_recursive_fun(complex_t C)
-
- {
- if (/* condition */) {
- /* code which perhaps does something to C */
- return my_recursive_fun(C);
- else {
- /* some other code */
- return C;
- }
- }
-
- If you passed and returned the structure by pointer, you not be able to use
- such recursion. You would have to declare an complex_t's on the stack, and
- explicitly make local copy, use the local copy recursively, and then copy the
- result over top of the original to pass the value back.
-
- This method is syntactically cleaner. A good compiler might optimize it by
- allowing the return complex_t to occupy the same spot as the parameter,
- depending on what is possible in the environment, thus making it probably no
- more wasteful of memory or cycles than the version which uses explicit copying.
- --
-
-